package com.ihcil.forecast.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ihcil.forecast.demo.exception.GlobalException;
import com.ihcil.forecast.demo.mapper.UserMapper;
import com.ihcil.forecast.demo.pojo.User;
import com.ihcil.forecast.demo.service.UserService;
import com.ihcil.forecast.demo.utils.CookieUtil;
import com.ihcil.forecast.demo.utils.MD5Utils;
import com.ihcil.forecast.demo.utils.UUIDUtil;
import com.ihcil.forecast.demo.vo.LoginVo;
import com.ihcil.forecast.demo.vo.RegistVo;
import com.ihcil.forecast.demo.vo.RespBean;
import com.ihcil.forecast.demo.vo.RespBeanEnum;
//import com.sun.org.apache.xpath.internal.operations.String;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author @baseException
 * @since 2021-12-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        // 根据手机号获取用户，手机号为主键`user_id`
        User user = userMapper.selectById(mobile);
        if (user == null) {
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        // 判断密码是否正确
        // 将前端传回来的密码 + 查到的用户salt进行一次MD5加密，再与数据库中的密码进行比对
        if (!MD5Utils.pass2DBpass(password, user.getSlat()).equals(user.getUserPwd())) {
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        // 生成cookie
        String ticket = UUIDUtil.uuid();
        // 将用户存在redis中的Key保存到session中
        request.getSession().setAttribute("userTicket", "user:" + ticket);
        // 将用户信息存入redis，并且设置过期时间为60min
        redisTemplate.opsForValue().set("user:" + ticket, user, 300, TimeUnit.SECONDS);
        CookieUtil.setCookie(request, response, "userTicket", ticket);

        return RespBean.success();
    }

    // 根据cookie获取用户
    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(userTicket))
            return null;
        User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);

        if (user != null)
            CookieUtil.setCookie(request, response, "userTicket", userTicket);

        return user;
    }

    @Override
    public RespBean regist(RegistVo registVo) {
        Long mobile = registVo.getMobile();
        String password = registVo.getPassword();
        String salt = registVo.getSalt();

        User user = userMapper.selectById(mobile);
        // 说明用户已存在
        if (user != null) {
            throw new GlobalException(RespBeanEnum.BIND_ERROR);
        }else {
            User user1 = new User();
            user1.setUserId(mobile);
            user1.setUserName(String.valueOf(mobile));
            user1.setUserPwd(MD5Utils.pass2DBpass(password, salt));
            user1.setSlat(salt);
            userMapper.insert(user1);
        }
        return RespBean.success();
    }
}
